package cn.ujn.medicalbackend.controller;

import cn.ujn.medicalbackend.common.BaseResponse;
import cn.ujn.medicalbackend.common.ErrorCode;
import cn.ujn.medicalbackend.common.ResultUtils;
import cn.ujn.medicalbackend.domain.Material;
import cn.ujn.medicalbackend.dto.MaterialQuery;
import cn.ujn.medicalbackend.exception.BusinessException;
import cn.ujn.medicalbackend.request.MaterialAddRequest;
import cn.ujn.medicalbackend.request.MaterialDeleteRequest;
import cn.ujn.medicalbackend.request.MaterialUpdateRequest;
import cn.ujn.medicalbackend.service.MaterialService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/material")
@CrossOrigin(origins = {"http://localhost:9092"})
public class MaterialController {
    @Autowired
    private MaterialService materialService;

    @GetMapping("/list/material")
    public BaseResponse<Page<Material>> getmaterialListByPage(MaterialQuery materialQuery) {
        if (materialQuery== null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        Material material = new Material();
        BeanUtils.copyProperties(materialQuery, material);
        QueryWrapper<Material> queryWrapper = new QueryWrapper<>(material);
        Page<Material> page = new Page<>(materialQuery.getPageNum(), materialQuery.getPageSize());
        Page<Material> tempPage = materialService.page(page, queryWrapper);
        Page<Material> resultPage = materialService.materialPageTransfer(tempPage);
        return ResultUtils.success(resultPage);
    }
    @PostMapping("/add")
    public BaseResponse<Long> addMaterial(@RequestBody MaterialAddRequest materialAddRequest, HttpServletRequest request) {
        if (materialAddRequest== null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }

        long materialId =materialService.addMaterial(materialAddRequest, request);
        return ResultUtils.success(materialId);
    }

    @PostMapping("/update")
    private BaseResponse<Boolean> updateMaterial(@RequestBody MaterialUpdateRequest materialUpdateRequest, HttpServletRequest request) {

        if (materialUpdateRequest == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        boolean result = materialService.updateMaterial(materialUpdateRequest,request);
        return ResultUtils.success(true);
    }

    @PostMapping("/delete")
    private BaseResponse<Boolean> deleteMaterial(@RequestBody MaterialDeleteRequest materialDeleteRequest, HttpServletRequest request) {
        if (materialDeleteRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result =  materialService.deleteMaterial(materialDeleteRequest, request);
        return ResultUtils.success(result);
    }
}
